package actions;

import SessionFactory.SessionFactoryUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import moderator.ModeratorControlPanel;
import beans.PreventSpamFormBean;
import dto.JfMembers;
import dto.Posts;
import dto.Topics;
import dto.TopicsHome;
import java.util.Date;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author Deena
 */
public class preventSpamAction extends org.apache.struts.actions.DispatchAction {

    ModeratorControlPanel mod = new ModeratorControlPanel();
    TopicsHome topicHome = new TopicsHome();

    public ActionForward preventSpam(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int postId = ((PreventSpamFormBean) form).getPostid();
        int topicid = ((PreventSpamFormBean) form).getTopicid();
        SessionFactoryUtil.startNew();
        Topics topic = topicHome.findById(topicid);
        JfMembers user = (JfMembers) request.getSession(false).getAttribute("userBean");
        boolean prevelge = mod.checkprevelage(getParentSection(topicid), user);
        if (prevelge) {
            Posts post = new Posts(postId);
            boolean b = mod.preventspam(post);
            response.sendRedirect("viewTopicAction.do?topic="+topicid);
            return null;
        } else {
            ActionErrors error = new ActionErrors();
            error.add("PrevError", new ActionMessage("Prev"));
            saveErrors(request, error);
            return new ActionForward("/viewTopicAction.do?topic="+topicid);
        }
    }
    public ActionForward splitTopic(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ActionForward forward;
        PreventSpamFormBean bean = (PreventSpamFormBean)form;
        Date date = new Date(bean.getNewName());
        int topicId = bean.getTopicid();
        Topics topics = new Topics();
        topics.setTopicid(topicId);

        JfMembers user = (JfMembers) request.getSession(false).getAttribute("userBean");
        boolean prevelge = mod.checkprevelage(getParentSection(topicId), user);
        if (prevelge) {
            mod.split(topics, date);
            forward = new ActionForward("/viewTopicAction.do?topic="+topicId);
        } else {
            ActionErrors error = new ActionErrors();
            error.add("PrevError", new ActionMessage("Prev"));
            saveErrors(request, error);
            forward = new ActionForward("/viewTopicAction.do?topic="+topicId);
        }
        return forward;
    }
    public ActionForward moveTopic(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ActionForward forward;
        PreventSpamFormBean bean = (PreventSpamFormBean)form;
        int topicId = bean.getTopicid();
        JfMembers user = (JfMembers) request.getSession(false).getAttribute("userBean");
        boolean prevelge = mod.checkprevelage(getParentSection(topicId), user) && mod.checkprevelage(bean.getSectionId(), user);
        if (prevelge) {
            mod.move(topicId, bean.getSectionId());
            forward = new ActionForward("/DisplayTopicAction.do?section="+ bean.getSectionId());
        } else {
            ActionErrors error = new ActionErrors();
            error.add("PrevError", new ActionMessage("Prev"));
            saveErrors(request, error);
            forward = new ActionForward("/viewTopicAction.do?topic="+topicId);
        }
        return forward;
    }
    public Integer getParentSection(int topicId){
        SessionFactoryUtil.startNew();
        Topics topics = topicHome.findById(topicId);
        Integer sec = topics.getSections().getSectionid();
        SessionFactoryUtil.finish();
        return sec;
    }
}
